package com.UnionPayCenter.distribution.manager.impl;

import com.UnionPayCenter.distribution.constants.DistributionConstants;
import com.UnionPayCenter.distribution.domain.DistributionUser;
import com.UnionPayCenter.distribution.entity.DistributionTemplateDO;
import com.UnionPayCenter.distribution.entity.UserDistributionDO;
import com.UnionPayCenter.distribution.manager.UserDistributionManager;
import com.UnionPayCenter.mapper.DistributionTemplateDOMapper;
import com.UnionPayCenter.mapper.UserDistributionDOMapper;
import com.UnionPayCenter.service.ServiceTemplate;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @author Noah
 * @since 1.0.0, 2019/07/03
 */
@Service
public class UserDistributionManagerImpl implements UserDistributionManager {

    @Autowired
    private DistributionTemplateDOMapper distributionTemplateDOMapper;

    @Autowired
    private UserDistributionDOMapper userDistributionDOMapper;

    @Autowired
    private ServiceTemplate serviceTemplate;


    /**
     * 初始化分销用户信息表
     *
     * @param user
     */
    @Override
    public UserDistributionDO initUserDistribution(DistributionUser user, Long accountId) {
        Date date = new Date();
        if (Objects.isNull(user) || Objects.isNull(user.getCounterpartyUserVo())) {
            return null;
        }
        DistributionTemplateDO distributionTemplateDO = distributionTemplateDOMapper.selectByShopId(user.getBId(), user.getShopId());
        if (Objects.isNull(distributionTemplateDO)) {
            return null;
        }
        UserDistributionDO exists = userDistributionDOMapper.selectByUserIdAndShopId(user.getBId(), user.getShopId(), user.getCounterpartyUserVo().getUserId().longValue());
        if (Objects.nonNull(exists)) {
            return exists;
        }
        UserDistributionDO userDistributionDO = new UserDistributionDO();
        userDistributionDO.setIdgen(String.valueOf(distributionTemplateDO.getIdgen()));
        userDistributionDO.setSType(DistributionConstants.STYPE);
        userDistributionDO.setSSn(null);
        userDistributionDO.setBId(distributionTemplateDO.getBId());
        userDistributionDO.setShopId(distributionTemplateDO.getShopId());
        userDistributionDO.setDistrId(distributionTemplateDO.getDistrId());
        userDistributionDO.setShareProp(distributionTemplateDO.getShareProp());
        userDistributionDO.setShareType(distributionTemplateDO.getShareType());
        userDistributionDO.setPreShareFlag(distributionTemplateDO.getPreShareFlag());
        userDistributionDO.setShareVal(distributionTemplateDO.getShareVal());
        userDistributionDO.setShareAmoLimit(distributionTemplateDO.getShareAmoLimit());
        userDistributionDO.setShareLvl(distributionTemplateDO.getShareLvl());
        userDistributionDO.setAccrualStart(distributionTemplateDO.getAccrualStart());
        userDistributionDO.setAccrualEnd(distributionTemplateDO.getAccrualEnd());
        userDistributionDO.setActLev(distributionTemplateDO.getActLev());
        userDistributionDO.setDisType(distributionTemplateDO.getDisType());
        userDistributionDO.setFlag(distributionTemplateDO.getFlag());
        userDistributionDO.setCreateTime(date);
        userDistributionDO.setUpdateTime(date);
        userDistributionDO.setActDate(date);
        userDistributionDO.setAccId(String.valueOf(accountId));
        userDistributionDO.setActNumRes(99999);
        //0邀请页、1二维码、2小程序,用户自主选择后更新
        userDistributionDO.setDistrMeth("1");
        userDistributionDO.setUserId(user.getCounterpartyUserVo().getUserId().longValue());
        userDistributionDO.setUserName(user.getCounterpartyUserVo().getNick());
        userDistributionDO.setUserMobile(user.getCounterpartyUserVo().getMobile());
        userDistributionDOMapper.insertSelective(userDistributionDO);
        return userDistributionDO;
    }

    /**
     * 获取支付分销用户信息
     *
     * @param bId    商户ID
     * @param shopId 店铺ID
     * @param userId 用户ID
     * @return
     */
    @Override
    public UserDistributionDO getPayUserDistributionByUserId(Long bId, Long shopId, Long userId) {
        if (Objects.isNull(bId) || Objects.isNull(shopId) || Objects.isNull(userId)) {
            return null;
        }
        return userDistributionDOMapper.selectByUserIdAndShopId(bId, shopId, userId);
    }


}
